package com.wing.sell.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.sell.model.entity.AgentDueSubject;
import com.wing.sell.model.entity.CustomerDueSubject;
import com.wing.sell.model.request.CustomerDueSubjectForm;
import com.wing.sell.model.response.AgentDueSubjectVO;
import com.wing.sell.model.response.CustomerDueSubjectVO;
import com.wing.sell.service.CustomerDueSubjectService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "Admin-客户尽调科目管理", tags = "#/sell/customerDueSubject")
@RestController("customerDueSubjectController")
@RequestMapping("sell/admin/customerDueSubject")
@RequiredArgsConstructor
public class CustomerDueSubjectController {

    private final CustomerDueSubjectService customerDueSubjectService;


    @ApiOperation(value = "查询所有客户尽调科目", notes = "搜索")
    @GetMapping("/list")
    public PageResult<CustomerDueSubjectVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "sort_number") String sort,
            @RequestParam(defaultValue = "asc") String order, Long subId, String name) {
        Page<CustomerDueSubject> pg = new Page<>(page, limit);
        Map queryParam = new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order", order);
        queryParam.put("subId", subId);
        queryParam.put("name", name);
        IPage<CustomerDueSubjectVO> list = customerDueSubjectService.pageList4app(pg, queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加客户尽调科目", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(CustomerDueSubjectForm.Add.class) @RequestBody CustomerDueSubjectForm customerDueSubjectForm) {
        if (customerDueSubjectService.add(customerDueSubjectForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改客户尽调科目", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(CustomerDueSubjectForm.Update.class) @RequestBody CustomerDueSubjectForm customerDueSubjectForm) {
        CustomerDueSubject customerDueSubject = customerDueSubjectService.getById(customerDueSubjectForm.getId());
        if (customerDueSubject == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(CustomerDueSubject.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(customerDueSubjectForm, customerDueSubject, copyOptions);
        if (customerDueSubjectService.update(customerDueSubject)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除客户尽调科目", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerDueSubjectId", value = "客户尽调科目id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long customerDueSubjectId) {
        if (customerDueSubjectService.delete(customerDueSubjectId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "启用", notes = "启用")
    @PostMapping("/enable")
    public JsonResult enable(Long id) {
        if (customerDueSubjectService.enable(id) > 0) {
            return JsonResult.success("启用成功");
        }
        return JsonResult.fail("启用失败");
    }

    @ApiOperation(value = "停用", notes = "停用")
    @PostMapping("/disable")
    public JsonResult disable(Long id) {
        if (customerDueSubjectService.disable(id) > 0) {
            return JsonResult.success("停用成功");
        }
        return JsonResult.fail("停用失败");
    }

}
